﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _1_6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int go = 0;
        foreach(var x in ListBox1.Items)
        {
            if (TextBox1.Text.Equals(x.ToString()))
            {
                go = 1;
                Response.Write("相同元素 不可以");
            }
        }
        if(go == 1)
        {
            return;
        }

        if(ListBox1.SelectedItem == null)
        {
            ListBox1.Items.Add(TextBox1.Text);
        }
        else
        {
            int x = ListBox1.SelectedIndex;
            ListBox1.Items.Insert(x,TextBox1.Text);
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
       int [] x =  ListBox1.GetSelectedIndices();
        int s = 0;
        foreach(int i in x)
        {
            ListBox1.Items.RemoveAt(i - s++);
        }
    }



    protected void Button3_Click(object sender, EventArgs e)
    {
        Label1.Text = ListBox1.Items[ListBox1.SelectedIndex].ToString();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        foreach(var x in ListBox1.Items)
        {
            Label1.Text += x.ToString() + "---";
        }
    }
}